package com.maas.util.mybatis.jpa.littlenb.common;

import java.util.Date;

/**
 * <p>
 * JpaLangTools
 * </p>
 *
 * @author zxm
 * @date 2021/08/13 16:09
 * @since 2021/08/13 16:09
 */
public class JpaLangTools {
    /**
     * Object转String
     */
    public static String trfNull(Object obj)
    {
        return obj==null?"":obj.toString();
    }

    public static String trfEmpty(Object obj,String defaultStr)
    {
        return trfNull(obj)==""?defaultStr:obj.toString();
    }
    /**
     * Object转long
     */
    public static long trflong(Object obj)
    {
        String str = trfNull(obj);
        return trfStrTolong(str);
    }

    public static Long trfLong(Object obj, Long defaultValue){
        String str = trfNull(obj);
        if(str==null||"".equals(str)){
            return defaultValue;
        }else{
            try{
                return Long.parseLong(str);
            }catch(NumberFormatException e){
                return defaultValue;
            }
        }
    }

    /**
     * String 转long
     *
     */
    public static long trfStrTolong(String obj){
        if(obj==null||"".equals(obj)){
            return 0L;
        }else{
            try{
                return Long.parseLong(obj);
            }catch(NumberFormatException e){
                return 0L;
            }
        }
    }


    /**
     * String 转long
     *
     */
    public static Long trfTolong(Object obj){
        if (obj instanceof Double) {
            Double num = (Double) obj;
            return num.longValue();
        }else if (obj instanceof String) {
            return trfStrTolong((String)obj);
        }
        try {
            Long l =(Long)obj;
            return l;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }



    /**
     * Object转float
     */
    public static float trfFloat(Object obj)
    {
        String str = trfNull(obj);
        return trfStrToFloat(str);
    }

    public static Float trfFloat(Object obj, Float defaultValue){
        String str = trfNull(obj);
        if(str==null||"".equals(str)){
            return defaultValue;
        }else{
            try{
                return Float.parseFloat(str);
            }catch(NumberFormatException e){
                return defaultValue;
            }
        }
    }

    public static float trffloat(Object obj, float defaultValue){
        String str = trfNull(obj);
        if(str==null||"".equals(str)){
            return defaultValue;
        }else{
            try{
                return Float.parseFloat(str);
            }catch(NumberFormatException e){
                return defaultValue;
            }
        }
    }

    /**
     * String 转float
     *
     */
    public static float trfStrToFloat(String obj){
        if(obj==null||"".equals(obj)){
            return 0L;
        }else{
            try{
                return Float.parseFloat(obj);
            }catch(NumberFormatException e){
                return 0L;
            }
        }
    }


    public static Date trfDate(Object obj, Date defaultValue)
    {

        if(obj==null){
            return defaultValue;
        }else{
            try{
                return (Date) obj;
            }catch(Exception e){
                return defaultValue;
            }
        }
    }


    /**
     * 对象转换为整数
     * @param obj ：对象
     * @return 整数，null为defaultValue(默认值，可以传null)
     */
    public static Integer trfInteger(Object obj, Integer defaultValue)
    {
        if (obj == null) {
            return defaultValue;
        } else {
            if ("".equals(obj.toString())) {
                return defaultValue;
            }
            try {
                return Integer.parseInt(obj.toString());
            } catch (NumberFormatException e) {
                return defaultValue;
            }
        }
    }

    public static Integer trfInteger(Object obj)
    {
        return trfInteger(obj, 0);
    }

    public static int trfInt(Object obj){
        Integer integer =trfInteger(obj);
        if(integer==null){
            return 0;
        }
        return integer.intValue();

    }

    public static int trfInt(Object obj, int defaultValue){
        Integer integer =trfInteger(obj,defaultValue);
        if(integer==null){
            return 0;
        }
        return integer.intValue();

    }

    public static int trfInt(Integer integer){
        if(integer==null){
            return 0;
        }
        return integer.intValue();

    }



    public static double trfDouble(Object obj, double defaultValue){
        String str = trfNull(obj);
        if(str==null||"".equals(str)){
            return defaultValue;
        }else{
            try{
                return Double.parseDouble(str);
            }catch(NumberFormatException e){
                return defaultValue;
            }
        }
    }
}
